/*
 *   Copyright 2006 Wei Lian <lianwei3@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
#include "header.h"
#include "callbacks.h"
#include "iochannel.h"
#include "iosignal.h"
#include "sound.h"
#include "loadsave.h"
int signal_pipe[2];
int sound_handler;
char new_url[64];
extern pid_t pid_player;
GtkTextBuffer *text_buf;
GtkWidget *entry_status;
GtkWidget *entry_player;
GtkLabel *label_player;
GtkWidget *button_player;
GtkAdjustment *adj_sound;
GtkWidget *button_attach;
GtkAdjustment *adj_text;

int
main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);
  GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 3);
  gtk_widget_set_usize (GTK_WIDGET (window), 350, 300);
  GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);
  GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  GtkWidget *label = gtk_label_new ("Player");
  gtk_widget_show (label);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  entry_player = gtk_entry_new ();
  gtk_widget_show (entry_player);
  gtk_box_pack_start (GTK_BOX (hbox), entry_player, TRUE, TRUE, 0);
//  gtk_entry_set_text (GTK_ENTRY (entry_player),
//                    "mplayer -ontop -geometry 100%:100%");
  hbox = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
  button_attach = gtk_toggle_button_new_with_label ("Attach");
  g_signal_connect (G_OBJECT (button_attach), "toggled",
		    G_CALLBACK (onbuttonattachtoggled), NULL);
  gtk_box_pack_start (GTK_BOX (hbox), button_attach, FALSE, FALSE, 0);
  gtk_widget_show (button_attach);
  button_player = gtk_toggle_button_new ();
  label_player = GTK_LABEL (gtk_label_new ("Player"));
  gtk_widget_show (GTK_WIDGET (label_player));
  gtk_container_add (GTK_CONTAINER (button_player),
		     GTK_WIDGET (label_player));
  g_signal_connect (G_OBJECT (button_player), "toggled",
		    G_CALLBACK (onbuttonplayertoggled), NULL);
  gtk_box_pack_start (GTK_BOX (hbox), button_player, FALSE, FALSE, 0);
  gtk_widget_show (button_player);
  /////////open mixer
  if ((sound_handler = open ("/dev/mixer", O_RDWR, 0)) == -1)
    {
      perror ("/dev/mixer");
      return -1;
    }
  adj_sound =
    GTK_ADJUSTMENT (gtk_adjustment_new
		    (gdouble (sound_read (sound_handler)), 0, 100, 1, 0, 0));
  gtk_signal_connect (GTK_OBJECT (adj_sound), "value_changed",
		      GTK_SIGNAL_FUNC (onadjustmentchanged), NULL);
  GtkWidget *hscale_sound = gtk_hscale_new (adj_sound);
  gtk_widget_show (hscale_sound);
  gtk_box_pack_start (GTK_BOX (hbox), hscale_sound, TRUE, TRUE, 0);
  gtk_scale_set_digits (GTK_SCALE (hscale_sound), 0);

  GtkWidget *scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
  adj_text =
    gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW
					 (scrolledwindow));
  gtk_widget_show (scrolledwindow);
  gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, TRUE, TRUE, 0);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
				  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow),
				       GTK_SHADOW_IN);
  GtkWidget *textview = gtk_text_view_new ();
  gtk_text_view_set_editable (GTK_TEXT_VIEW (textview), FALSE);
  gtk_widget_show (textview);
  gtk_container_add (GTK_CONTAINER (scrolledwindow), textview);
  text_buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
  entry_status = gtk_entry_new ();
  gtk_entry_set_editable (GTK_ENTRY (entry_status), FALSE);
  gtk_widget_show (entry_status);
  gtk_box_pack_start (GTK_BOX (vbox), entry_status, FALSE, FALSE, 0);
  gtk_widget_show (window);
  ///////////////timer
  g_timeout_add (3000, timerOut, NULL);
  ///////////////pipe
  if (pipe (signal_pipe))
    {
      perror ("pipe");
      exit (1);
    }
  fcntl (signal_pipe[1], F_SETFL,
	 fcntl (signal_pipe[1], F_GETFL) | O_NONBLOCK);
  /////////install signal handler
  signal (SIGCHLD, pipe_signals);
  /////create channel
  Channel iosignal;
  create_iochannel (iosignal, signal_pipe[0], handle_io_signal);
  ////set locale
  setlocale (LC_CTYPE, "zh_CN.GBK");
  ////load config
  load_config ();
  //////////assign initial value
  pid_player = -1;
  strcpy (new_url, "http://121.1.1.1:1000/live.asf");
  //////main loop
  gtk_main ();
  ////////////close aumix
  close (sound_handler);
  return 0;
}
